//
//  StatusCellRetweetedView.swift
//  Weibo
//
//  Created by 明丽 on 16/1/20.
//  Copyright © 2016年 明丽. All rights reserved.
//

import UIKit
import SnapKit

class StatusCellRetweetedView: UIView {
    var bottomConstant: Constraint?
    //定义外部变量
    var retweeted_status: Status?{
        didSet {
            retweetedLabel.text = retweeted_status?.text
            pictureView.imageURLs = retweeted_status?.imageURLs
            self.bottomConstant?.uninstall()
            
            if let urls = retweeted_status?.imageURLs{
                print(urls.count)
                //设置底部约束相对于配图视图的底部
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstant = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstant = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
            
            
        }
        
    }
    
    
    //自定义入口,转发微博
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置视图
    private func setupUI() {
        //手动创建view的背景颜色是透明的
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(retweetedLabel)
        addSubview(pictureView)
        //设置约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetedLabel.snp_left)
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            
        }
        //添加自动计算转发微博的行高一条约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstant = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
            
        }
        
        
    }
    //懒加载视图
    private lazy var retweetedLabel: UILabel = UILabel(title: "转发微博:这就是我,不一样的烟火", fontSize: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    
    private lazy var pictureView: StayusCellPictureView = StayusCellPictureView()
}
